package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
 
import model.Admin;
import model.Fdy;
import model.Student;
import model.SuGuan;
import model.WuYe;
import utils.DBconn;
public class MsgDao {
	
	// 数据库连接
			// 删除原有的getConnection方法
//——————————————————————————————————————————————————————管理员———————————————————————————————————————————————————————————————————————————————————————
			//查询管理员信息
			public List<Admin> getAloneAdminMessage(String id) {
			    List<Admin> personMessage = new ArrayList<>();
			    String sql = "SELECT * FROM gyadmin WHERE id = ?";
			 
			    try (Connection conn = DBconn.getConnection();
			         PreparedStatement stmt = conn.prepareStatement(sql)) {
			 
			        stmt.setString(1, id);
			        try (ResultSet rs = stmt.executeQuery()) {
			            while (rs.next()) {
			                Admin admin = new Admin(); // 假设 Admin 类存在且适用
			                admin.setName(rs.getString("name"));
			                admin.setId(rs.getString("id"));
			                admin.setPassword(rs.getString("password")); // 注意：通常密码不应以明文形式存储或返回
			                //admin.setPower(rs.getString("power"));
			                personMessage.add(admin); // 将 Admin 对象添加到列表中
			            }
			        }
			    } catch (SQLException e) {
			        // 可以选择记录日志或重新抛出异常
			        e.printStackTrace(); // 仅为调试，生产环境中应使用更健壮的错误处理机制
			        
			    }
			    return personMessage;
			}
//——————————————————————————————————————————————————————物业———————————————————————————————————————————————————————————————————————————————————————
            //查询物业信息
			public List<WuYe> getAloneWuYeMessage(String id) {
			    List<WuYe> personMessage = new ArrayList<>();
			    String sql = "SELECT * FROM wy WHERE id = ?";
			 
			    try (Connection conn = DBconn.getConnection();
			         PreparedStatement stmt = conn.prepareStatement(sql)) {
			 
			        stmt.setString(1, id);
			        try (ResultSet rs = stmt.executeQuery()) {
			            while (rs.next()) {
			            	WuYe wy = new WuYe(); // 假设 WuYe 类存在且适用
			            	wy.setName(rs.getString("name"));
			            	wy.setId(rs.getString("id"));
			            	wy.setPassword(rs.getString("password")); // 注意：通常密码不应以明文形式存储或返回
			            	//wy.setPower(rs.getString("power"));
			                personMessage.add(wy); // 将 WuYe对象添加到列表中
			            }
			        }
			    } catch (SQLException e) {
			        // 可以选择记录日志或重新抛出异常
			        e.printStackTrace(); // 仅为调试，生产环境中应使用更健壮的错误处理机制
			        
			    }
			    return personMessage;
			}
//—————————————————————————————————————————————————辅导员————————————————————————————————————————————————————————————————————————————————————————————
            //查询辅导员信息
			public List<Fdy> getAloneFdyMessage(String id) {
			    List<Fdy> personMessage = new ArrayList<>();
			    String sql = "SELECT * FROM fdy WHERE id = ?";

			    try (Connection conn = DBconn.getConnection();
			         PreparedStatement stmt = conn.prepareStatement(sql)) {

			        stmt.setString(1, id);
			        try (ResultSet rs = stmt.executeQuery()) {
			            while (rs.next()) {
			                Fdy fdy = new Fdy();
			                fdy.setName(rs.getString("name"));
			                fdy.setId(rs.getString("id"));

			                // setCollege方法确实需要字符串数组
			                // 如果只需要一个学院名称，可以考虑直接存储为字符串
			                String collegeName = rs.getString("class");
			                if (collegeName != null) {
			                    String[] colleges = { collegeName };
			                    fdy.setCollege(colleges);
			                } else {
			                    fdy.setCollege(new String[0]); // 或者null，取决于setCollege的实现
			                }

			                personMessage.add(fdy); // 将Fdy对象添加到列表中
			            }
			        }
			    } catch (SQLException e) {
			        // 记录日志或重新抛出异常
			        e.printStackTrace();
			        // 可以选择抛出运行时异常，或者根据需求处理
			        throw new RuntimeException("数据库查询失败", e);
			    }

			    // 注意：如果ID是唯一的，这里可能只会返回一个对象或空列表
			    return personMessage;
			}
			/*
			 * public List<Fdy> getAloneFdyMessage(String id) { List<Fdy> personMessage =
			 * new ArrayList<>(); String sql = "SELECT * FROM fdy WHERE id = ?";
			 * 
			 * try (Connection conn = getConnection(); PreparedStatement stmt =
			 * conn.prepareStatement(sql)) {
			 * 
			 * stmt.setString(1, id); try (ResultSet rs = stmt.executeQuery()) { while
			 * (rs.next()) { Fdy fdy = new Fdy(); // 假设 Fdy 类存在且适用
			 * fdy.setName(rs.getString("name")); fdy.setId(rs.getString("id")); //
			 * 将单个字符串值转换为字符串数组 String collegeName = rs.getString("college"); String[]
			 * colleges = { collegeName }; fdy.setCollege(colleges);
			 * 
			 * } } } catch (SQLException e) { // 记录日志或重新抛出异常，这里只是简单地打印堆栈跟踪
			 * e.printStackTrace(); } return personMessage; }
			 */
//—————————————————————————————————————————————————宿管————————————————————————————————————————————————————————————————————————————————————————————
            //查询宿管信息
			public List<SuGuan> getAloneSuGuanMessage(String id) {
			    List<SuGuan> personMessage = new ArrayList<>();
			    String sql = "SELECT * FROM sg WHERE id = ?";
			 
			    try (Connection conn = DBconn.getConnection();
			         PreparedStatement stmt = conn.prepareStatement(sql)) {
			 
			        stmt.setString(1, id);
			        try (ResultSet rs = stmt.executeQuery()) {
			            while (rs.next()) {
			            	SuGuan sg = new SuGuan(); // 假设 SuGuan 类存在且适用
			            	sg.setName(rs.getString("name"));
			            	sg.setId(rs.getString("id"));
			            	sg.setBuilding(rs.getString("build")); 
			            
			                personMessage.add(sg); // 将 SuGuan对象添加到列表中
			            }
			        }
			    } catch (SQLException e) {
			        // 可以选择记录日志或重新抛出异常
			        e.printStackTrace(); // 仅为调试，生产环境中应使用更健壮的错误处理机制
			        
			    }
			    return personMessage;
			}
//—————————————————————————————————————————————————学生————————————————————————————————————————————————————————————————————————————————————————————
            //查询宿管信息
			public List<Student> getAloneStudentMessage(String id) {
			    List<Student> personMessage = new ArrayList<>();
			    String sql = "SELECT * FROM student WHERE id = ?";
			 
			    try (Connection conn = DBconn.getConnection();
			         PreparedStatement stmt = conn.prepareStatement(sql)) {
			 
			        stmt.setString(1, id);
			        try (ResultSet rs = stmt.executeQuery()) {
			            while (rs.next()) {
			            	Student student = new Student(); // 假设 Student 类存在且适用
			            	student.setName(rs.getString("name"));
			            	student.setId(rs.getString("id"));
			            	student.setClassName(rs.getString("class"));
			            	student.setRoomid(rs.getString("roomid"));
			                personMessage.add(student); // 将 Student对象添加到列表中
			            }
			        }
			    } catch (SQLException e) {
			        // 可以选择记录日志或重新抛出异常
			        e.printStackTrace(); // 仅为调试，生产环境中应使用更健壮的错误处理机制
			        
			    }
			    return personMessage;
			}
}
